package com.yochi.pictureseditor.workerthreads;

import com.yochi.pictureseditor.Processor;
import com.yochi.pictureseditor.effects.ColorEnum;
import com.yochi.pictureseditor.effects.Effect;
import com.yochi.pictureseditor.effects.EffectApplier.ApplyEffectListener;
import com.yochi.pictureseditor.effects.EffectEnum;
import com.yochi.pictureseditor.effects.filters.Filter;
import com.yochi.pictureseditor.effects.filters.FilterEnum;
import com.yochi.pictureseditor.effects.filters.filtersimpl.FilterPersonalized;
import com.yochi.pictureseditor.exceptions.EffectNotCreatedException;
import com.yochi.pictureseditor.exceptions.NullImageException;
import com.yochi.pictureseditor.factories.EffectFactory;
import com.yochi.pictureseditor.images.Image;
import com.yochi.pictureseditor.tools.Color;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ApplyEffectWorkerThread extends AsyncTask<Image, Void, Image> {
	
	private static final String TAG = "ApplyEffectWorkerThread";
	
	public static final int MSG_CNF = 1000;
	
	private ApplyEffectListener listener;
	private Message msg = null;
	
	private EffectEnum effect = null;
	private FilterEnum filterEnum = null;
	
	private ColorEnum color = null;
	private Color color1 = null;
	private Color color2 = null;
	private Filter filter = null;
	
	private int parameter = 0;
	private int p1 = -1; 
	private int p2 = -1; 
	private int p3 = -1;
	
	public ApplyEffectWorkerThread(EffectEnum effect, int parameter, ApplyEffectListener listener) {
		this.effect = effect;
		this.parameter = parameter;
		this.listener = listener;
	}
	
	public ApplyEffectWorkerThread(EffectEnum effect, ColorEnum color, ApplyEffectListener listener) {
		this.effect = effect;
		this.color = color;
		this.listener = listener;
	}
	
	public ApplyEffectWorkerThread(EffectEnum effect, int p1, int p2, ApplyEffectListener listener) {
		this.p1 = p1; this.p2 = p2;
		this.effect = effect;
		this.listener = listener;
	}
	
	public ApplyEffectWorkerThread(EffectEnum effect, int p1, int p2, int p3, ApplyEffectListener listener) {
		this.p1 = p1; this.p2 = p2; this.p3 = p3;
		this.effect = effect;
		if(effect == EffectEnum.COLORIZE) {
			this.color = ColorEnum.PERSONALIZED;
		}
		this.listener = listener;
	}
	
	public ApplyEffectWorkerThread(EffectEnum effect, int parameter, Color color1, Color color2, ApplyEffectListener listener) {
		this.effect = effect;
		this.parameter = parameter;
		this.color1 = color1; 
		this.color2 = color2;
		this.listener = listener;
	}
	
	public ApplyEffectWorkerThread(EffectEnum effect, ApplyEffectListener listener) {
		this.effect = effect;
		this.listener = listener;
	}
	
	public ApplyEffectWorkerThread(FilterEnum filter, ApplyEffectListener listener) {
		this.filterEnum = filter;
		this.listener = listener;
	}
	
	public ApplyEffectWorkerThread(FilterPersonalized filter, ApplyEffectListener listener) {
		this.filterEnum = FilterEnum.PERSONALIZED;
		this.filter = filter;
		this.listener = listener;
	}
	
	@Override
	protected Image doInBackground(Image... img) {
		Effect mEffect = null;
		try { 
			mEffect = new EffectFactory().build(img[0], effect, filterEnum, color, color1, color2, filter, parameter, p1, p2, p3);
			Log.d(TAG, "onClick:"+mEffect.toString());
			return mEffect.apply();
			
		} catch (NullImageException e) { 
			Log.e(TAG, String.valueOf(e)); 
			
		} catch (EffectNotCreatedException e) {
			Log.e("Effect", "EffectNotCreatedException : ",e);
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(Image result) {
		super.onPostExecute(result);
		msg = effectHandler.obtainMessage(MSG_CNF, (Object) "Ended !");
		effectHandler.sendMessage(msg);
		if(listener != null) {
			listener.effectApplied(effect, result);
		}
		this.cancel(true);
	}
	
	
	  /////////////
	 // HANDLER //
	/////////////

	private static final Handler effectHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_CNF: 
			if (Processor.mProgressDialog.isShowing())  {
				Processor.mProgressDialog.dismiss();
			}
			break;
			default:
				break;
	}	}	};
	
}